package com.qiangesoft.wechat.core.service.impl;

import com.qiangesoft.wechat.core.service.IWxTemplateMessageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 模板消息服务
 *
 * @author qiangesoft
 * @date 2023-09-07
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class WxTemplateMessageServiceImpl implements IWxTemplateMessageService {

    private final WxMpService wxMpService;

    @Override
    public String sendTemplateMsg(String openId, String templateId, List<WxMpTemplateData> templateDataList) throws WxErrorException {
        WxMpTemplateMessage message = WxMpTemplateMessage.builder()
                .toUser(openId)
                .templateId(templateId)
                .data(templateDataList)
                .build();
        return wxMpService.getTemplateMsgService().sendTemplateMsg(message);
    }

    @Override
    public String sendTemplateMsg(String openId, String url, String templateId, List<WxMpTemplateData> templateDataList) throws WxErrorException {
        WxMpTemplateMessage message = WxMpTemplateMessage.builder()
                .toUser(openId)
                .url(url)
                .templateId(templateId)
                .data(templateDataList)
                .build();
        return wxMpService.getTemplateMsgService().sendTemplateMsg(message);
    }

}
